package com.xuy.ridis.redisdemo.DemoWeb;

import com.xuy.ridis.redisdemo.utils.HttpClientUtil;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.hibernate.validator.constraints.LuhnCheck;
import org.omg.CORBA.portable.ValueOutputStream;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @version v1.0
 * @类描述：
 * @项目名称：
 * @包名： com.xuy.ridis.redisdemo.DemoWeb
 * @类名称：demo
 * @创建人：YIHANG.XU
 * @创建时间：2021/10/309:46 下午
 * @修改人：YIHANG.XU
 * @修改时间：2021/10/309:46 下午
 * @修改备注：
 * @Copyright
 */
@Controller
@RequestMapping("/demo")
public class demo {

    @RequestMapping("/toHome")
    public void toHome(HttpServletRequest request, HttpServletResponse response) {
        try {
            RestTemplate sllRestTemplate = HttpClientUtil.getSllRestTemplate();
             List<String> login = login(sllRestTemplate);





             ;




            String token = login.get(0).split("XSRF-TOKEN=")[1];
            String session = login.get(1).split("SESSION")[1];

            String tokenm = token.substring(0, token.length() - 8);
            String sessionm = session.substring(0, session.length() - 8);
            Cookie cookie1 = new Cookie("XSRF-TOKEN", tokenm); //创建Cookie对象，指定名字和值
            Cookie cookie = new Cookie("SESSION", sessionm); //创建Cookie对象，指定名字和值
            Cookie cookie2 = new Cookie("user", "admin"); //创建Cookie对象，指定名字和值
            response.setHeader("X-XSRF-TOKEN",tokenm);
            //User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36 Edg/92.0.902.78
            response.setHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36 Edg/92.0.902.78");

            response.addCookie(cookie1);
            response.addCookie(cookie);
            response.addCookie(cookie2);
            response.sendRedirect("https://39.108.64.200:8089/#/home");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //登录系统
    public static List<String> login(RestTemplate sllRestTemplate) {

        String loginUrl = "https://39.108.64.200:5558/helmetProxy/login";


        String userName = "admin";  //账号
        String password = "Kc123123";  //密码
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);

            MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
            form.add("username", userName);
            form.add("password", password);
            form.add("lang", "zh_CN");
            HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(form, headers);
            ResponseEntity<String> exchange = sllRestTemplate.exchange(loginUrl, HttpMethod.POST, httpEntity, String.class);
            HttpHeaders headers1 = exchange.getHeaders();
            List<String> list = headers1.get("Set-Cookie");

            return list;


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
